package com.code.bolg.controller;

import com.code.bolg.bean.Photos;
import com.code.bolg.config.SysConfig;
import com.code.bolg.enums.ResultEnum;
import com.code.bolg.service.PhotosService;
import com.code.bolg.utils.GifDecoder;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/photos")
public class PhotosController {
    @Autowired
    private SysConfig sysConfig;

    @Autowired
    private PhotosService photosService;
    private static final DateFormat format = new SimpleDateFormat("yyyy/MM/dd/");
    private int width = 200;
    private int height = 200;
    private String thumb = "thumb/";


    @RequestMapping("/list")
    public Object init() {
        Map<String, Object> map = new HashMap<>();
        return map;
    }

    @RequestMapping("/upload")
    public Object upload(MultipartFile file) throws IOException {
        if (file.isEmpty()) {//判断文件为null
            return ResultEnum.FILEISNULL.getMessage();
        }
        String fileName = file.getOriginalFilename();//获取文件名
        String format = this.format.format(new Date());
        String path = sysConfig.getUploadPath() + format;   //自定义文件存放位置
        String suffix = fileName.substring(fileName.lastIndexOf("."));//获取文件后缀名
        String newName = UUID.randomUUID().toString() + suffix;
        File dest = new File(path);
        if (!dest.exists()) {
            dest.mkdirs();
        }
        if(!new File(path + thumb).exists()){
            new File(path + thumb).mkdir();
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path + newName);
            out.write(file.getBytes());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        File filel = new File(path + newName);
        //判断图片，并确定文件后缀
        if (sysConfig.getImgtypes().contains(suffix)) {
            if (ResultEnum.JPG.getMessage().equals(suffix)) {
                BufferedImage originalImage = null;
                try {
                    originalImage = ImageIO.read(new FileInputStream(filel));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Thumbnails.of(originalImage)
                        .size(width, height)
                        .toFile(path + thumb+newName);
            } else if (ResultEnum.GIF.getMessage().equals(suffix)) {
                FileInputStream data = new FileInputStream(filel);
                GifDecoder.GifImage gif = GifDecoder.read(data);
                Thumbnails.of(data)
                        .size(width, height)
                        .toFile(path + thumb+newName);
            }
        }

        Photos photos = new Photos();
        photos.setCreatDate(new Date());
        photos.setName(newName);
        photos.setSuffix(suffix);
        photos.setClassify("照片墙");
        photos.setSrc(sysConfig.getImgPath()+format+newName);
        photos.setThumb(sysConfig.getImgPath()+format+ thumb+newName);
        Photos save = photosService.save(photos);
        return save;
    }
}
